package com.vehicle.controller;

import com.vehicle.bean.DataGridView;
import com.vehicle.bean.PageVo;
import com.vehicle.bean.ResultDto;
import com.vehicle.pojo.ApplyUse;
import com.vehicle.pojo.Check;
import com.vehicle.pojo.Driver;
import com.vehicle.pojo.User;
import com.vehicle.service.ApplyUseService;
import com.vehicle.service.CarExamineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/car")
public class ApplyUserController extends DataTimeController{

    @Autowired
    private ApplyUseService applyUseService;

    @Autowired
    private CarExamineService carExamineService;

    //查询所有申请
    @RequestMapping("selectAllApplyUse")
    @ResponseBody
    public DataGridView selectAllApplyUse(String status, PageVo pageVo){
        return applyUseService.findAllApplyUse(pageVo,status);
    }
    //删除申请
    @RequestMapping("delAllApplyUse")
    @ResponseBody
    public ResultDto delAllApplyUse(Integer applyid){
        ResultDto resultDto = new ResultDto();
        try {
            resultDto.setCode(1001);
            resultDto.setMsg("删除申请成功");
            applyUseService.deApplyUse(applyid);
        }catch (Exception e){
            resultDto.setMsg("删除申请失败");
            resultDto.setCode(-1001);
        }
        return resultDto;
    }

    //跳转添加申请页面
    @RequestMapping("applyAdd")
    public ModelAndView applyAdd(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/page/car/ApplyAdd.jsp");
        return modelAndView;
    }

    @RequestMapping("doApplyAdd")
    @ResponseBody
    public ResultDto doApplyAdd(ApplyUse applyUse, HttpSession session){
        ResultDto resultDto = new ResultDto();
        try{
            User user = (User) session.getAttribute("USER_SESSION");
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String str = sdf.format(date);
            int random = (int) (Math.random()*100000);
            String applyno = str + random ;
            applyUse.setApplyno(applyno);
            applyUse.setApplyuser(user.getUid());
            applyUse.setDeptid(user.getDept().getDeptid());
            applyUse.setStatus(3);
            applyUseService.applyAdd(applyUse);
            resultDto.setCode(1001);
            resultDto.setMsg("添加成功！");
        }catch (Exception e){
            e.printStackTrace();
            resultDto.setCode(-1001);
            resultDto.setMsg("添加失败!");
        }
        return resultDto;
    }

    //动态显示司机信息
    @RequestMapping("findAllDriver")
    @ResponseBody
    public ResultDto findAllDriver(){
        ResultDto resultDto = new ResultDto();
        try {
            List<Driver> driverList = applyUseService.findAllDriver();
            resultDto.setCode(1001);
            resultDto.setData(driverList);
        }catch (Exception e){
            e.printStackTrace();
            resultDto.setCode(-1001);
        }
        return  resultDto;
    }

    //跳转预览界面
    @RequestMapping("checkApply")
    public ModelAndView checkApply(Integer applyid){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/page/car/ApplyCheck.jsp");
        ApplyUse applyUse = applyUseService.selectApplyUseById(applyid);
        modelAndView.addObject("applyUse",applyUse);
        Check check = carExamineService.selectCheckByApplyid(applyid);
        modelAndView.addObject("check",check);
        return modelAndView;
    }


}
